var util = require('../../utils/util.js');
var api = require('../../config/api.js');

var app = getApp();

Page({
  data: {
    shopInfo:null,
    type: 0,
    searchValue:'',
    supplyList: [],
    page: 1,
    limit: 10,
    totalPages: 1,
    hasLogin: false
  },
  init:function(){
    this.setData({
      supplyList: [],
      page: 1,
      limit: 10,
      totalPages: 1
    });
  },
  switchTab: function(event) {
    let type = event.currentTarget.dataset.index;
    this.setData({
      supplyList: [],
      type,
      page: 1,
      limit: 10,
      totalPages: 1
    });
    this.getSupplyList();
  },
  getSupplyList(reachBottom=false) {
    wx.showLoading({
      title: '加载中...',
    });
    let that = this;
    util.request(api.SupplyList, {
      type: that.data.type,
      page: that.data.page,
      limit: that.data.limit,
      searchValue:that.data.searchValue
    }).then(function(res) {
      if (res.errno === 0) {
        let supplyList = res.data.list.map(item => {
          item.publishDt = util.formatTime2(new Date(item.publishDt),"Y/M/D");
          item.herbListImage && (item.herbListImage01 =  item.herbListImage.split(',')[0])
          return item;
        });
        //触底加载新数据并保留老数据
        if(reachBottom){
          supplyList = [...that.data.supplyList,...supplyList]
        }
        that.setData({
          supplyList: supplyList,
          totalPages: res.data.pages
        });
        wx.hideLoading();
      }
    }).finally(() => {
      wx.hideLoading();
    });
  },
  onLoad: function(options) {
    this.init();
    this.getSupplyList();
  },
  onReady: function() {

  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.init();
    this.getSupplyList();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  onReachBottom() {
    if (this.data.totalPages > this.data.page) {
      this.setData({
        page: this.data.page + 1
      });
    } else {
      return false;
    }
    this.getSupplyList(true);
  },
  onShow: function() {
    if (app.globalData.hasLogin) {
      this.getSupplyList();
      console.log(2)
      let shopInfo = wx.getStorageSync('shopInfo');
      this.setData({
        shopInfo:shopInfo
      });
    }
    this.setData({
      hasLogin: app.globalData.hasLogin
    });
  },
  onHide: function() {
    // 页面隐藏

  },
  onUnload: function() {
    // 页面关闭
  },
  goLogin() {
    wx.navigateTo({
      url: "/pages/auth/login/login"
    });
  },
  openSupply(event) {
    let that = this;
    let index = event.currentTarget.dataset.index;
    let valueId = this.data.supplyList[index].id;

    //触摸时间距离页面打开的毫秒数  
    var touchTime = that.data.touchEnd - that.data.touchStart;
    //如果按下时间大于350为长按  
    if (touchTime > 350) {
      wx.showModal({
        title: '',
        content: '确定删除吗？',
        success: function(res) {
          if (res.confirm) {

            util.request(api.SupplyAddOrDelete, {
              type: that.data.type,
              valueId: valueId
            }, 'POST').then(function(res) {
              if (res.errno === 0) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success',
                  duration: 2000
                });
                that.data.supplyList.splice(index, 1)
                that.setData({
                  supplyList: that.data.supplyList
                });
              }
            });
          }
        }
      })
    } else {
      var prefix = "/pages/supplyDetail/supplyDetail?id="
      wx.navigateTo({
        url: prefix + valueId,
      });
    }
  },
  //按下事件开始  
  touchStart: function(e) {
    let that = this;
    that.setData({
      touchStart: e.timeStamp
    })
  },
  //按下事件结束  
  touchEnd: function(e) {
    let that = this;
    that.setData({
      touchEnd: e.timeStamp
    })
  },
  onChange(e) {
    this.setData({
      searchValue: e.detail,
    });
  },
  onSearch() {
    this.getSupplyList();
  },
  addSupply(){
    wx.navigateTo({
      url: '/pages/supplyAdd/supplyAdd'
    })
  },
})